import {
  Body,
  Controller, Get,
  Headers, Param, ParseUUIDPipe, Patch,
  Post, Put, Query, UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AdminService } from './admin.service';
import {
  AdminCreateDto,
  AdminIndexDto,
  AdminLoginDto, AdminUpdateDto, AdminUpdateMinePasswordDto,
} from '@/module/admin/admin.dto';
import { AdminIndexResult, AdminLoginResult, AdminMineResult } from './admin.result';
import { getPager } from "@/common/pager-response";
import { PageResult } from "common/interface";
import { AdminModel } from "@/module/admin/admin.model";
import { Admin } from "@/common/auth/admin.decorator";
import { AdminAuthGuard } from "@/common/auth/admin-auth.guard";
import { ApiPaginatedOkResponse } from "@/common/pager-response/api-paginated-ok-response.decorator";

@Controller('/v1/admins')
@ApiTags('Admin_Admin')
export class AdminV1AdminController {
  constructor(private readonly adminService: AdminService) {
  }

  @Post('/login')
  @ApiOperation({ summary: '管理员登录' })
  @ApiOkResponse({
    type: AdminLoginResult,
  })
  async login(
    @Body() body: AdminLoginDto,
  ): Promise<AdminLoginResult> {
    const token = await this.adminService.login(body);
    return { token };
  }

  @Get('/')
  @ApiOperation({ summary: '查全部' })
  @ApiPaginatedOkResponse(AdminIndexResult)
  async index(@Query() query: AdminIndexDto): Promise<PageResult<AdminIndexResult>> {
    const res = await this.adminService.adminIndex(query);
    return getPager(...res);
  }

  @Get('/mine')
  @ApiOperation({ summary: '查自己' })
  @ApiOkResponse({ type: AdminMineResult })
  @UseGuards(AdminAuthGuard)
  async mine(@Admin() admin: AdminModel): Promise<AdminMineResult | null> {
    const res = await this.adminService.adminMine(admin.uuid);
    return res;
  }

  @Put('/mine/password')
  @ApiOperation({ summary: '修改自己密码' })
  @UseGuards(AdminAuthGuard)
  async updateMinePassword(@Body() body: AdminUpdateMinePasswordDto, @Admin() admin: AdminModel): Promise<void> {
    const res = await this.adminService.adminUpdateMinePassword(body, admin.uuid);
    return res;
  }

  @Post('/')
  @ApiOperation({ summary: '新增' })
  @UseGuards(AdminAuthGuard)
  async create(@Body() body: AdminCreateDto): Promise<void> {
    await this.adminService.adminCreate(body);
    return;
  }

  @Patch('/:uuid')
  @ApiOperation({ summary: '修改' })
  @UseGuards(AdminAuthGuard)
  async update(
    @Body() body: AdminUpdateDto,
    @Param('uuid', ParseUUIDPipe) uuid: string,
  ): Promise<void> {
    await this.adminService.adminUpdate(body, uuid);
    return;
  }

  @Patch('/mine')
  @ApiOperation({ summary: '修改' })
  @UseGuards(AdminAuthGuard)
  async updateMine(
    @Body() body: AdminUpdateMinePasswordDto,
    @Admin() admin: AdminModel
  ): Promise<void> {
    await this.adminService.adminUpdate(body, admin.uuid);
    return;
  }

  @Post('/logout')
  @ApiOperation({ summary: '管理员登出' })
  @UseGuards(AdminAuthGuard)
  async logout(@Headers('token') token: string): Promise<void> {
    await this.adminService.logout(token);
    return;
  }
}
